package com.fastloan.safe.core.param;

import com.fastloan.safe.core.param.bean.IndexTypeBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.fastloan.safe.core.param.RoleTypeEnum.Group.GROUP_CUSTOMER;
import static com.fastloan.safe.core.param.RoleTypeEnum.Group.GROUP_SYS;
import static com.fastloan.safe.core.param.RoleTypeEnum.Group.GROUP_USER;

/**
 * @author rankai
 *         createTime 2017-10-2017/10/17 19:51
 */
public enum RoleTypeEnum {

    SYS_ROOT(0, "系统管理员", GROUP_SYS),
    ADMIN(1, "顶级机构管理员", GROUP_USER),
    ORG_ADMIN(2, "子机构管理员", GROUP_USER),
    MANAGER(3, "客户经理", GROUP_USER),
    AUDITOR(4, "电核员", GROUP_USER),
    EXPERT(5, "审贷专家", GROUP_USER),
    MAKE_LOANS(6, "放款专员", GROUP_USER),
    CUSTOMER(15, "客户", GROUP_CUSTOMER),
    SON_CUSTOMER(16, "子客户", GROUP_CUSTOMER);

    private int code;

    private String name;

    private int group;

    RoleTypeEnum(int code, String name, int group) {
        this.code = code;
        this.name = name;
        this.group = group;
    }

    public class Group {
        public static final int GROUP_SYS = 1;
        public static final int GROUP_USER = 2;
        public static final int GROUP_CUSTOMER = 3;
    }

    public static List<IndexTypeBean> selectByGroup() {
        RoleTypeEnum[] values = RoleTypeEnum.values();
        List<IndexTypeBean> list = new ArrayList<>(values.length);
        for (RoleTypeEnum value : values) {
            list.add(new IndexTypeBean(value.code, value.name));
        }
        return list;
    }

    public static List<IndexTypeBean> selectByGroup(RoleTypeEnum... exclude) {
        RoleTypeEnum[] values = RoleTypeEnum.values();
        List<IndexTypeBean> list = new ArrayList<>(values.length);
        for (RoleTypeEnum value : values) {
            if (exclude != null) {
                if (Arrays.asList(exclude).contains(value)) {
                    continue;
                }
            }
            list.add(new IndexTypeBean(value.code, value.name));
        }
        return list;
    }

    public static List<IndexTypeBean> selectByGroup(int... group) {
        RoleTypeEnum[] values = RoleTypeEnum.values();
        List<IndexTypeBean> list = new ArrayList<>(values.length);
        for (RoleTypeEnum value : values) {
            for (int i : group) {
                if (value.group == i) {
                    list.add(new IndexTypeBean(value.code, value.name));
                }
            }
        }
        return list;
    }

    public static String getNameByCode(int code) {
        RoleTypeEnum[] values = RoleTypeEnum.values();
        for (RoleTypeEnum value : values) {
            if (value.getCode() == code) {
                return value.getName();
            }
        }
        return null;
    }

    public int getCode() {
        return code;
    }

    public String getName() {
        return name;
    }
}
